<template>
  <div class="waiting-time-chart" style="width: 375px; height: 300px;">
    <div id="chart" style="width: 100%; height: 100%;"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts';
import { onMounted, onUnmounted } from 'vue';

export default {
  name: 'Jiaohao',
  setup() {
    let chart = null;

    onMounted(() => {
      const chartDom = document.getElementById('chart');
      if (chartDom) {
        chart = echarts.init(chartDom);

        const option = {
          backgroundColor: '#0f2035',
          title: {
            text: '累计平均叫号等待时长TOP5',
            textStyle: {
              color: '#fff',
              fontSize: 16,
              fontWeight: 'bold'
            },
            left: 'center',
            top: '10px',
            padding: [0, 0, 0, 0],
            itemGap: 10
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'shadow'
            }
          }, 
          grid: {
            left: '10%',
            right: '10%',
            bottom: '15%',
            top: '20%'
          },
          xAxis: {
            type: 'category',
            data: ['单位A', '单位B', '单位C', '单位D', '单位E'],
            axisLabel: {
              color: '#fff',
              fontSize: 12
            },
            axisLine: {
              lineStyle: {
                color: '#999'
              }
            },
            axisTick: {
              show: false
            }
          },
          yAxis: {
            type: 'value',
            max: 40,
            interval: 10,
            axisLabel: {
              color: '#fff',
              fontSize: 12
            },
            axisLine: {
              lineStyle: {
                color: '#999'
              }
            },
            splitLine: {
              lineStyle: {
                color: '#333'
              }
            }
          },
          series: [
            {
              name: '等待时长',
              type: 'bar',
              barWidth: '30%',
              data: [36, 27, 21, 14, 8],
              label: {
                show: true,
                position: 'top',
                formatter: '{c}',
                color: '#fff',
                fontSize: 12
              },
              itemStyle: {
                color: function(params) {
                  const colors = ['#e94c4c', '#ff9900', '#9b9bfc', '#00e4ff', '#00baff'];
                  return colors[params.dataIndex];
                }
              }
            }
          ]
        };

        chart.setOption(option);

        window.addEventListener('resize', () => {
          if (chart) {
            chart.resize();
          }
        });
      }
    });

    onUnmounted(() => {
      if (chart) {
        chart.dispose();
      }
    });

    return {};
  }
};
</script>

<style scoped>
.waiting-time-chart {
  width: 375px;
  height: 300px;
  border-radius: 8px;
  overflow: hidden;
  background-color: #0f2035;
}
</style>